using System.Collections.Specialized;
using System.Text;
using Twipler.App.Render;
using Twipler.App.Render.Tree;

namespace Twipler.App.AsyncCommands
{
	public class DelayedRenderCommand : AsyncCommand
	{
		private string _postbackId;
        
		public DelayedRenderCommand(NameValueCollection collection)
		{
			_postbackId = collection["id"];            
		}

		public override string Render()
		{
			PageProducer page = Current.PageProducer;
			if (page == null)
				return "unexpected error";

			DelayItem render = page.FindByPostbackId(_postbackId) as DelayItem;

			if (render == null)
				return "Could not find postback id " + _postbackId;
            
			StringBuilder html = new StringBuilder();
			render.RenderChild(html, page.MachineEnvironment);

			return html.ToString();
		}
	}
}